using System;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Runtime.CompilerServices;
namespace Microsoft.Phone.Controls.Maps.Core
{
	public class TileSourceCollection : ObservableCollection<TileSource>
	{
		public event EventHandler<TileSourcePropertyChangedEventArgs> ItemPropertyChanged
		{
			[MethodImpl(32)]
			add
			{
				this.ItemPropertyChanged = (EventHandler<TileSourcePropertyChangedEventArgs>)Delegate.Combine(this.ItemPropertyChanged, value);
			}
			[MethodImpl(32)]
			remove
			{
				this.ItemPropertyChanged = (EventHandler<TileSourcePropertyChangedEventArgs>)Delegate.Remove(this.ItemPropertyChanged, value);
			}
		}
		public TileSourceCollection()
		{
			base.add_CollectionChanged(new NotifyCollectionChangedEventHandler(this.TileSourceCollection_CollectionChanged));
		}
		private void TileSourceCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
		{
			this.AddTileSourceEventHandler(e.get_NewItems());
			this.RemoveTileSourceEventHandler(e.get_OldItems());
		}
		private void AddTileSourceEventHandler(IList tileSources)
		{
			if (tileSources != null)
			{
				IEnumerator enumerator = tileSources.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						TileSource tileSource = (TileSource)enumerator.get_Current();
						tileSource.PropertyChanged += new PropertyChangedEventHandler(this.TileSource_PropertyChanged);
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
			}
		}
		private void RemoveTileSourceEventHandler(IList tileSources)
		{
			if (tileSources != null)
			{
				IEnumerator enumerator = tileSources.GetEnumerator();
				try
				{
					while (enumerator.MoveNext())
					{
						TileSource tileSource = (TileSource)enumerator.get_Current();
						tileSource.PropertyChanged -= new PropertyChangedEventHandler(this.TileSource_PropertyChanged);
					}
				}
				finally
				{
					IDisposable disposable = enumerator as IDisposable;
					if (disposable != null)
					{
						disposable.Dispose();
					}
				}
			}
		}
		private void TileSource_PropertyChanged(object sender, PropertyChangedEventArgs e)
		{
			EventHandler<TileSourcePropertyChangedEventArgs> itemPropertyChanged = this.ItemPropertyChanged;
			if (itemPropertyChanged != null)
			{
				TileSourcePropertyChangedEventArgs tileSourcePropertyChangedEventArgs = new TileSourcePropertyChangedEventArgs((TileSource)sender, e.get_PropertyName());
				itemPropertyChanged.Invoke(this, tileSourcePropertyChangedEventArgs);
			}
		}
	}
}
